package net.oschina.kjtang.qywx.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.oschina.kjtang.qywx.api.entity.Department;
import net.oschina.kjtang.qywx.api.service.WxDepartmentService;
import net.oschina.kjtang.qywx.common.component.httpclient.HttpClientComponent;
import net.oschina.kjtang.qywx.common.component.httpclient.HttpResult;
import net.oschina.kjtang.qywx.common.constants.RCodeConstants;
import net.oschina.kjtang.qywx.common.constants.WxAPIConstants;
import net.oschina.kjtang.qywx.common.exception.RCodeException;
import net.oschina.kjtang.qywx.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kaijun on 2017/12/2.
 */
@Service
public class WxDepartmentServiceImpl extends BaseServiceImpl<Department> implements WxDepartmentService {

    @Autowired
    private HttpClientComponent httpClientComponent;

    @Override
    public int addDepartment(Department department, String accessToken) throws RCodeException {
        try{
            String departJson = JSON.toJSONString(department);
            System.out.println(departJson);
            HttpResult httpResult = httpClientComponent.doPostJson(WxAPIConstants.DEPARTMENT_CREATE_URL.replace("ACCESS_TOKEN",accessToken),departJson);
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        logger.info("[新增部门]", "addDepartment response:{}", new Object[]{jsonObject.toJSONString()});
                        throw  new RCodeException(errcode,new RuntimeException(errmsg));
                    }else{
                        int id = jsonObject.getIntValue("id");
                        return id;
                    }
                }else{
                    throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException("新增部门失败"));
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }

        return 0;

    }

    @Override
    public void updateDepartment(Department department, String accessToken) throws RCodeException {
        try{
            String departJson = JSON.toJSONString(department);
            System.out.println(departJson);
            HttpResult httpResult = httpClientComponent.doPostJson(WxAPIConstants.DEPARTMENT_UPDATE_URL.replace("ACCESS_TOKEN",accessToken),departJson);
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        logger.info("[修改部门]", "updateDepartment response:{}", new Object[]{jsonObject.toJSONString()});
                        throw  new RCodeException(errcode,new RuntimeException(errmsg));
                    }
                }else{
                    throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException("部门更新失败"));
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }
    }

    @Override
    public void deleteDepartment(String departId, String accessToken) throws RCodeException {
        try{
            HttpResult httpResult = httpClientComponent.doGet(WxAPIConstants.DEPARTMENT_DELETE_URL.replace("ACCESS_TOKEN",accessToken).replace("ID",departId));
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        logger.info("[删除部门]", "deleteDepartment response:{}", new Object[]{jsonObject.toJSONString()});
                        throw  new RCodeException(errcode,new RuntimeException(errmsg));
                    }
                }else{
                    throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException("部门删除成功"));
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }
    }

    @Override
    public List<Department> getDepartmentList(String departId,String accessToken) throws RCodeException {
        List<Department> departments = new ArrayList<>();
        try{
            HttpResult httpResult = httpClientComponent.doGet(WxAPIConstants.DEPARTMENT_QUERY_URL.replace("ACCESS_TOKEN",accessToken).replace("ID",departId));
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    String departList = jsonObject.getString("department");
                    departments = JSON.parseObject(departList,departments.getClass());
                    if(errcode!=0){
                        logger.info("[查询所有部门列表]", "deleteDepartment response:{}", new Object[]{jsonObject.toJSONString()});
                        throw  new RCodeException(errcode,new RuntimeException(errmsg));
                    }
                }else{
                    throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException("部门删除成功"));
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }
        return departments;
    }
}
